clear all

*
* Clean 2016
*
use "source/externals/orig/anes/2016/anes_timeseries_2016.dta", clear
* full sample post-election weight
gen weight = V160102

* CSES
gen cses_like_D     = V162283
replace cses_like_D = . if cses_like_D < 0
gen cses_like_R     = V162284
replace cses_like_R = . if cses_like_R < 0

* ANES
gen anes_feel_D = V161095
replace anes_feel_D = . if anes_feel_D < 0
gen anes_feel_R = V161096
replace anes_feel_R = . if anes_feel_R < 0

gen anes_party_R = (V161155 == 2)
gen anes_party_D = (V161155 == 1)

gen anes_polar     = (anes_feel_R - anes_feel_D) if anes_party_R == 1
replace anes_polar = (anes_feel_D - anes_feel_R) if anes_party_D == 1

gen cses_polar_anes_pty     = 10 * (cses_like_R - cses_like_D) if anes_party_R == 1
replace cses_polar_anes_pty = 10 * (cses_like_D - cses_like_R) if anes_party_D == 1

* Drop Sample
keep if anes_polar != . & cses_polar_anes_pty != . & weight > 0
keep anes_* cses_* weight
gen year = 2016
tempfile temp2016
save `temp2016'


*
* Clean 2012
*
use "source/externals/orig/anes/2012/anes_timeseries_2012.dta", clear
* weight for full sample
gen weight = weight_full

* CSES
gen cses_like_D     = cses_dptylike
replace cses_like_D = . if cses_like_D < 0
gen cses_like_R     = cses_rptylike
replace cses_like_R = . if cses_like_R < 0

* ANES
gen anes_feel_D = ft_dem
replace anes_feel_D = . if anes_feel_D < 0
gen anes_feel_R = ft_rep
replace anes_feel_R = . if anes_feel_R < 0

gen anes_party_R = (pid_self == 2)
gen anes_party_D = (pid_self == 1)

gen anes_polar     = (anes_feel_R - anes_feel_D) if anes_party_R == 1
replace anes_polar = (anes_feel_D - anes_feel_R) if anes_party_D == 1

gen cses_polar_anes_pty     = 10 * (cses_like_R - cses_like_D) if anes_party_R == 1
replace cses_polar_anes_pty = 10 * (cses_like_D - cses_like_R) if anes_party_D == 1

* Drop Sample
keep if anes_polar != . & cses_polar_anes_pty != . & weight > 0
keep anes_* cses_* weight
gen year = 2012
tempfile temp2012
save `temp2012'



*
* Clean 2008
*
use "source/externals/orig/anes/2008/anes_timeseries_2008.dta", clear
* post-election sample weight
gen weight = V080102

* CSES
gen cses_like_D     = V085187a
replace cses_like_D = . if cses_like_D < 0
gen cses_like_R     = V085187b
replace cses_like_R = . if cses_like_R < 0

* ANES
gen anes_feel_D = V083044a
replace anes_feel_D = . if anes_feel_D < 0
gen anes_feel_R = V083044b
replace anes_feel_R = . if anes_feel_R < 0

gen anes_party_R = (V083097 == 2)
gen anes_party_D = (V083097 == 1)

gen anes_polar     = (anes_feel_R - anes_feel_D) if anes_party_R == 1
replace anes_polar = (anes_feel_D - anes_feel_R) if anes_party_D == 1

gen cses_polar_anes_pty     = 10 * (cses_like_R - cses_like_D) if anes_party_R == 1
replace cses_polar_anes_pty = 10 * (cses_like_D - cses_like_R) if anes_party_D == 1

* Drop Sample
keep if anes_polar != . & cses_polar_anes_pty != . & weight > 0
keep anes_* cses_* weight
gen year = 2008
tempfile temp2008
save `temp2008'


*
* Clean 2004
*
use "source/externals/orig/anes/2004/anes2004TS.dta", clear
* post-election sample weight
gen weight = V040102

* CSES
gen cses_like_D     = V045257
replace cses_like_D = . if cses_like_D > 10
gen cses_like_R     = V045258
replace cses_like_R = . if cses_like_R > 10

* ANES
gen anes_feel_D = V043049
replace anes_feel_D = . if anes_feel_D > 100
gen anes_feel_R = V043050
replace anes_feel_R = . if anes_feel_R > 100

gen anes_party_R = (V043114 == 1)
gen anes_party_D = (V043114 == 2)

gen anes_polar     = (anes_feel_R - anes_feel_D) if anes_party_R == 1
replace anes_polar = (anes_feel_D - anes_feel_R) if anes_party_D == 1

gen cses_polar_anes_pty     = 10 * (cses_like_R - cses_like_D) if anes_party_R == 1
replace cses_polar_anes_pty = 10 * (cses_like_D - cses_like_R) if anes_party_D == 1

* Drop Sample
keep if anes_polar != . & cses_polar_anes_pty != . & weight > 0
keep anes_* cses_* weight
gen year = 2004
tempfile temp2004
save `temp2004'


*
* Clean 1996
*
use "source/externals/orig/anes/1996/nes96.dta", clear
* Time-series weight (post)
gen weight = V960005B

* CSES
gen cses_like_D     = V961470
replace cses_like_D = . if cses_like_D > 10
gen cses_like_R     = V961471
replace cses_like_R = . if cses_like_R > 10

* ANES
gen anes_feel_D = V960292
replace anes_feel_D = . if anes_feel_D > 100
gen anes_feel_R = V960293
replace anes_feel_R = . if anes_feel_R > 100

gen anes_party_R = (V960417 == 2)
gen anes_party_D = (V960417 == 1)

gen anes_polar     = (anes_feel_R - anes_feel_D) if anes_party_R == 1
replace anes_polar = (anes_feel_D - anes_feel_R) if anes_party_D == 1

gen cses_polar_anes_pty     = 10 * (cses_like_R - cses_like_D) if anes_party_R == 1
replace cses_polar_anes_pty = 10 * (cses_like_D - cses_like_R) if anes_party_D == 1

* Drop Sample
keep if anes_polar != . & cses_polar_anes_pty != . & weight > 0
keep anes_* cses_* weight
gen year = 1996
tempfile temp1996
save `temp1996'


** AGGREGATE
use `temp1996', clear
append using `temp2004'
append using `temp2008'
append using `temp2012'
append using `temp2016'


* Bootstrap
program define get_reg, rclass
	preserve
	collapse (mean) anes_polar cses_polar_anes_pty [weight = weight], by(year)
	
	reg anes_polar year
	local anes_slope = _b[year]
	
	reg cses_polar_anes_pty year
	local cses_slope = _b[year]
	restore
	return scalar diff = `anes_slope' - `cses_slope'
end

bootstrap r(diff), reps(500) strata(year) seed(2020) : get_reg
mat table = r(table)
matrix_to_txt, matrix(table) saving("build/descriptive/anes_cses_bootstrap.txt") title("<tab:anes_cses_bootstrap>")

* Collapse and save
collapse (mean) anes_polar cses_polar_anes_pty [weight = weight], by(year)
outfile using "build/descriptive/anes_cses_comparison.txt", comma

